package com.kujie.config;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.memory.ChatMemoryRepository;
import org.springframework.ai.tool.ToolCallbackProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * 客户端 配置
 *
 * @author check
 * @date 2025-07-24
 */
@Configuration
public class ChatClientConfig {
    @Autowired
    private DataSource dataSource;

//    @Autowired
//    private MilvusVectorStore milvusVectorStore;

//    @Autowired
//    private RetrievalAugmentationAdvisor retrievalAugmentationAdvisor;

    @Autowired
    private SimpleLoggerAdvisor simpleLoggerAdvisor;

//    @Autowired
//    private SafeGuardAdvisor safeGuardAdvisor;

    @Autowired
    private ChatMemoryRepository chatMemoryRepository;

//    @Bean
//    public ChatMemory mysqlChatMemoryRepository() {
//        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
//        ChatMemoryRepository chatMemoryRepository = JdbcChatMemoryRepository.builder()
//                .jdbcTemplate(jdbcTemplate)
//                .dialect(new MysqlChatMemoryRepositoryDialect())
//                .transactionManager(new JdbcTransactionManager(dataSource))
//                .build();
//
//        return MessageWindowChatMemory.builder()
//                .chatMemoryRepository(chatMemoryRepository)
//                .maxMessages(10)
//                .build();
//    }

    @Bean
    public ChatClient buildChatClient(ChatClient.Builder ollamaChatModel) {
        return ollamaChatModel.build();
    }

    @Bean
    public ChatClient buildCurrentChatClient(ChatClient.Builder ollamaChatModel,
                                             ToolCallbackProvider tools) {

        return ollamaChatModel
                .defaultTools()
                .defaultToolCallbacks(tools.getToolCallbacks())
                .defaultAdvisors(
//                        MessageChatMemoryAdvisor.builder(chatMemory).build(),
//                        new QuestionAnswerAdvisor(milvusVectorStore),
//                        PromptChatMemoryAdvisor.builder(chatMemory).build(),
//                        retrievalAugmentationAdvisor,
                        simpleLoggerAdvisor)
//                        safeGuardAdvisor)
                .build();
    }
}
